import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:nft_shop/res/global.dart';
import 'package:url_launcher/url_launcher.dart';

class Utils {

  /// 打开链接
  static Future<void> launchWebURL(String url) async {
    if (await canLaunch(url)) {
      if (Global.isIOS) {
        launch(url, forceSafariVC: false, forceWebView: true);
        return;
      }
      if (Global.isAndroid) {
        launch(url);
      }
    } else {
      EasyLoading.showToast('打开链接失败！');
    }
  }

  static Future? pushPage(BuildContext context, Widget page) {
    if (context == null || page == null) return null;
    return Navigator.push(context, new CupertinoPageRoute<void>(builder: (ctx) => page));
  }

  static Future<T?> showElasticDialog<T>({
    required BuildContext context,
    bool barrierDismissible = true,
    required WidgetBuilder builder,
  }) {
    return showGeneralDialog(
      context: context,
      pageBuilder: (BuildContext buildContext, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        final Widget pageChild = Builder(builder: builder);
        return SafeArea(
          child: pageChild,
        );
      },
      barrierDismissible: barrierDismissible,
      barrierLabel: MaterialLocalizations
          .of(context)
          .modalBarrierDismissLabel,
      barrierColor: Colors.black54,
      transitionDuration: const Duration(milliseconds: 550),
      transitionBuilder: _buildDialogTransitions,
    );
  }

  static Widget _buildDialogTransitions(BuildContext context,
      Animation<double> animation, Animation<double> secondaryAnimation,
      Widget child) {
    return FadeTransition(
      opacity: CurvedAnimation(
        parent: animation,
        curve: Curves.easeOut,
      ),
      child: SlideTransition(
        position: Tween<Offset>(
            begin: const Offset(0.0, 0.3),
            end: Offset.zero
        ).animate(CurvedAnimation(
          parent: animation,
          curve: const ElasticOutCurve(0.85),
          reverseCurve: Curves.easeOutBack,
        )),
        child: child,
      ),
    );
  }

  static String constructTime(int value) {
    int secondTime = value;
    int minuteTime = 0;
    int hourTime = 0;
    int dayTime = 0;
    int mouthTime = 0;
    int yearTime = 0;
    if (secondTime > 60) {
      minuteTime = secondTime ~/ 60;
      secondTime = secondTime % 60;
      if (minuteTime > 60) {
        hourTime = minuteTime ~/ 60;
        minuteTime = minuteTime % 60;
        if (hourTime > 24) {
          dayTime = hourTime ~/ 24;
          hourTime = hourTime % 24;
          if (dayTime > 30) {
            mouthTime = dayTime ~/ 30;
            dayTime = dayTime % 30;
            if (mouthTime > 12) {
              yearTime = mouthTime ~/ 12;
              mouthTime = mouthTime % 12;
            }
          }
        }
      }
    }
    String result = "$secondTime 秒";
    if (minuteTime > 0) {
      result = " $minuteTime 分 $result";
    }
    if (hourTime > 0) {
      result = "$hourTime 小时 $result";
    }
    if (dayTime > 0) {
      result = "$dayTime 天 $result";
    }
    if (mouthTime > 0) {
      result = "$mouthTime 个月 $result";
    }
    if (yearTime > 0) {
      result = " $yearTime 年 $result";
    }
    return result;
  }
}


